package top.dingyingying.springboot3.service.api.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import top.dingyingying.springboot3.entity.api.HistoryResult;
import top.dingyingying.springboot3.service.BaseApiService;
import top.dingyingying.springboot3.service.api.HistoryService;
import top.dingyingying.springboot3.util.JsonUtils;

@Slf4j
@RequiredArgsConstructor
@Service
public class historyServiceImpl extends BaseApiService implements HistoryService {
    private final RestTemplate restTemplate;

    @Override
    public HistoryResult getHistory() {
        String url = String.format(HISTORY_URL, "1");
        String result = restTemplate.exchange(url, HttpMethod.GET, buildHttpEntity(), String.class).getBody();
        if (StringUtils.hasText(result)){
            return JsonUtils.parse(result, HistoryResult.class);
        }
        return null;
    }
}
